//
//  WBVisitorsView.swift
//  WB
//
//  Created by macssss on 16/7/1.
//  Copyright © 2016年 macssss. All rights reserved.
//

import UIKit

class WBVisitorsView: UIView {
  
    //注册
  lazy var registerButton: UIButton = UIButton.cz_textButton(
    "注册",
    fontSize: 16,
    normalColor: UIColor.orange(),
    highlightedColor: UIColor.darkGray(),
    backgroundImageName: "common_button_white_disable")
    
 //登录
  lazy var loginButton: UIButton = UIButton.cz_textButton(
    "登录",
    fontSize: 16,
    normalColor: UIColor.darkGray(),
    highlightedColor: UIColor.orange(),
    backgroundImageName: "common_button_white_disable")
    
 

    
    var visitionInfo: [String : String]? {
        didSet {
            //守护
            guard let imageName = visitionInfo?["imageName"],
                message = visitionInfo?["message"] else {
                    
                    return
            }
            
            tipLabel.text = message
            
            if imageName == "" {
                startAnimation()
                return
            }
            
            iconView.image = UIImage(named: imageName)
            
            //别的界面不需要显示房子
            housetView.isHidden = true
            shadeView.isHidden = true
        }
        
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func  startAnimation() {
        
        let anmiation = CABasicAnimation(keyPath: "transform.rotation")
        
        anmiation.toValue = 2 * M_PI
        anmiation.repeatCount = MAXFLOAT
        anmiation.duration = 10
        
        //动画完成不删除
        anmiation.isRemovedOnCompletion = false
        
        iconView.layer.add(anmiation, forKey: nil)
    }
    
    //设置访客视图信息
    func setupInfo(dict: [String: String]) {
        
          }
    
    //视图
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //遮罩视图
    private lazy var shadeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //房子
    private lazy var housetView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //描述
    private lazy var tipLabel: UILabel = UILabel.cz_label(
        withText: "你想什么呢？ 我这什么都有， 你想买啥就买啥你想什么呢？ 我这什么都有， 你想买啥就买啥",fontSize: 14,
               color: UIColor.darkGray())
    
}

extension WBVisitorsView {
    
    
    func setupUI () {
        backgroundColor = UIColor.cz_color(withHex: 0xEDEDED)
        
        addSubview(iconView)
        addSubview(shadeView)
        addSubview(housetView)
        
        addSubview(registerButton)
        addSubview(loginButton)
        addSubview(tipLabel)
       
        //设置文本居中
        tipLabel.textAlignment = .center
        //取消aoutresizing
        for v in subviews {
            
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        // 3. 自动布局
        let margin: CGFloat = 20.0

        
        // 视图布局
        addConstraint(NSLayoutConstraint(item: iconView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant:  0))

        addConstraint(NSLayoutConstraint(
                                        item: iconView,
                                        attribute: .centerY,
                                        relatedBy: .equal,
                                        toItem: self,
                                        attribute: .centerY,
                                        multiplier: 1.0,
                                        constant: -60))
        
        // 房子布局
        
        addConstraint(NSLayoutConstraint(
                                            item: housetView,
                                            attribute: .centerX,
                                            relatedBy: .equal,
                                            toItem: iconView,
                                            attribute: .centerX,
                                            multiplier: 1.0,
                                            constant: 0))
        
        addConstraint(NSLayoutConstraint(
                                            item: housetView,
                                            attribute: .centerY,
                                            relatedBy: .equal,
                                            toItem: iconView,
                                            attribute: .centerY,
                                            multiplier: 1.0,
                                            constant: 0))
        
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: margin))
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 236))
        
        // 4> 注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .left,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .left,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: margin))
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 100))
        
        // 5> 登录按钮
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .right,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .right,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: margin))
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: registerButton,
                                         attribute: .width,
                                         multiplier: 1.0,
                                         constant: 0))
        
        let viewDict = ["shadeView": shadeView,
                        "registerButton":registerButton]
       let metrics = ["spacing": 20]
        
        addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "H:|-0-[shadeView]-0-|",
            options: [],
            metrics: nil,
            views: viewDict))
        
        addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "V:|-0-[shadeView]-(spacing)-[registerButton]",
            options: [],
            metrics: metrics,
            views: viewDict))
    
    }
    
}














